package cn.qyd.concurrentcollection;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * @author 邱运铎
 * @date 2024-03-22 21:10
 */
public class ArrayBlockQueueDemo {
//    private static ArrayBlockingQueue<Integer> queue = new ArrayBlockingQueue<>(10,true);
    private static LinkedBlockingQueue<Integer> queue = new LinkedBlockingQueue(10);
    public static void main(String[] args) {
        new Thread(new Producer()).start();
        new Thread(new Consumer()).start();
    }

    static class Producer implements Runnable {

        @Override
        public void run() {
            for (int i = 0; i < 100; i++) {
                try {
                    queue.put(i);
                    System.out.println("生产者生产了： " + i);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    static class Consumer implements Runnable {

        @Override
        public void run() {
            for (int i = 0; i < 100; i++) {
                try {
                    Integer take = queue.take();
                    System.out.println("消费者消费了： " + take);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
